package com.tiger.security.sample3;

import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

public class Client {
    public static void main(String args[]) {
        try {
            if(args.length != 2) {
                System.out.println("Input User-Name and Password!");
                System.exit(-1);
            }
            LoginContext lc = doLogin(args[0], args[1]);
            InitialContext ctx = new InitialContext();
            Object obj = ctx.lookup("comp/env/ejb/Security3TestBean");
            Security3TestHome securityTestHome =
                (Security3TestHome)PortableRemoteObject.narrow(
                obj, Security3TestHome.class);
            Security3Test securityTest = securityTestHome.create();
            System.out.println(securityTest.method1());
            securityTest.remove();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    private static LoginContext doLogin(String username, String password) throws LoginException {
        SimpleCallbackHandler handler = new SimpleCallbackHandler(username, password);
        LoginContext lc = new LoginContext("other", handler);
        //LoginContext lc = new LoginContext("database-login", handler);
        lc.login();
        return lc;
    }
}
